﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TecBaseObjects.Models;

namespace TecData.Layer {

    internal class TpAccountingDataLayer : BaseSqlDataLayer {

        #region Members

        private const string loadTaxesStatement = "dbo.GetTaxCodes";
        private const string loadProductsStatement = "Products.GetProducts";
        private const string saveAccountStatement = @"accounting.SaveAccount";
        private const string saveAccount2CompanyStatement = @"accounting.SaveAccount2Company";
        private const string updateAccount2CompanyStatement = @"accounting.UpdateAccount2Company";

        #endregion

        #region Load

        public string GetAccountName(string accountNo, string company) {
            //string ret = "";
            string sql = "dbo.GetAccountName";
            using (IDataReader reader = this.GetDataReaderFromStoredProcedure(sql, new SqlParameter("@accountNo", accountNo), new SqlParameter("@company", company))) {
                return (reader.Read() ? GetAccountNameFromReader(reader) : null);
            }
            //return ret;
        }

        public List<CashSaleProductsModel> LoadProducts() {
            List<CashSaleProductsModel> myRet = new List<CashSaleProductsModel>();
            using (DataTable dt = this.GetDataTableFromStoredProcedure(loadProductsStatement)) {
                foreach (DataRow dr in dt.Rows) {
                    CashSaleProductsModel pm = new CashSaleProductsModel();
                    pm.Categorie = (string)dr["Categorie"];
                    pm.ContributionMargin = Convert.ToDouble(dr["ContributionMargin"]);
                    pm.CostPrice = Convert.ToDouble(dr["CostPrice"]);
                    if (dr["ItemNumber"].ToString() != "") {
                        pm.ItemNumber = (string)dr["ItemNumber"];
                    }
                    pm.ListPrice = Convert.ToDouble(dr["ListPrice"]);
                    pm.Manufacturer = (string)dr["Manufacturer"];
                    if (dr["ManufacturerOrgKey"].ToString() != "") {
                        pm.ManufacturerOrgKey = Convert.ToInt32(dr["ManufacturerOrgKey"]);
                    }
                    pm.ProductID = Convert.ToInt32(dr["ProductID"]);
                    pm.ProductName = (string)dr["ProductName"];
                    pm.Provider = (string)dr["Provider"];
                    if (dr["ProviderOrgKey"].ToString() != "") {
                        pm.ProviderOrgKey = Convert.ToInt32(dr["ProviderOrgKey"]);
                    }
                    pm.Rabatt1 = Convert.ToDouble(dr["Rabatt1"]);
                    pm.Rabatt2 = Convert.ToDouble(dr["Rabatt2"]);
                    pm.SalesPrice = Convert.ToDouble(dr["SalesPrice"]);
                    pm.Skonto = Convert.ToDouble(dr["Skonto"]);
                    pm.SubCategorie = (string)dr["SubCategorie"];
                    pm.SurCharge = Convert.ToDouble(dr["SurCharge"]);
                    pm.TaxCode = Convert.ToByte(dr["TaxCode"]);
                    pm.Unity = (string)dr["Unity"];
                    myRet.Add(pm);
                }
            }
            return myRet;
        }

        public List<TaxesModel> LoadTaxes() {
            List<TaxesModel> myRet = new List<TaxesModel>();
            using (DataTable dt = this.GetDataTableFromStoredProcedure(loadTaxesStatement)) {
                foreach (DataRow dr in dt.Rows) {
                    TaxesModel tm = new TaxesModel();
                    tm.TaxCodeID = Convert.ToByte(dr["TaxCodeID"]);
                    tm.TaxCode = (string)dr["TaxCode"];
                    tm.TaxRate = Convert.ToDouble(dr["TaxRate"]);
                    myRet.Add(tm);
                }
            }
            return myRet;
        }

        #endregion

        #region Save

        public virtual int SaveAccount(int accountID, int accountNumber, string accountName, string description = null, string details = null, string text = null, bool sk = false) {
            int ret = 0;
            using (IDataReader reader = this.GetDataReaderFromStoredProcedure(saveAccountStatement, 
                new SqlParameter("@AccountID", accountID),
                new SqlParameter("@AccountNumber", accountNumber),
                new SqlParameter("@AccountName", accountName),
                new SqlParameter("@Description", description),
                new SqlParameter("@Details", details),
                new SqlParameter("@Text", text),
                new SqlParameter("@SK", sk)
                )) {
                if (reader.Read()) {
                    if (this.GetInt32(reader, "AccountID") != 0) {
                        ret = this.GetInt32(reader, "AccountID");
                    }
                    if (this.GetInt32(reader, "Return") != 0) {
                        return 0;
                    }
                }
            }
            return ret;
        }

        public virtual int SaveAccount2Company(int accountID, int orgKey) {
            int ret = 0;
            using (IDataReader reader = this.GetDataReaderFromStoredProcedure(saveAccount2CompanyStatement,
                new SqlParameter("@AccountID", accountID),
                new SqlParameter("@OrgKey", orgKey)
                )) {
                if (reader.Read()) {
                    if (this.GetInt32(reader, "AccountID") != 0) {
                        ret = this.GetInt32(reader, "AccountID");
                    }
                    if (this.GetInt32(reader, "Return") != 0) {
                        return 0;
                    }
                }
            }
            return ret;
        }

        #endregion

        #region Update

        public virtual void UpdateAccount2Company(int accountID, int orgKey) {
            int ret = this.ExecuteStoredProcedure(updateAccount2CompanyStatement,
                new SqlParameter("@AccountID", accountID),
                new SqlParameter("@OrgKey", orgKey)
                );

        }

        #endregion

        #region Functions

        private string GetAccountNameFromReader(IDataReader reader) {
            return this.GetString(reader, "AccountName");
        }

        #endregion

    }
}
